package org.nicocube.airain.domain;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import org.junit.BeforeClass;
import org.junit.Test;
import org.nicocube.airain.domain.client.character.Action;
import org.nicocube.airain.domain.client.character.ActionType;
import org.nicocube.airain.domain.client.character.Expertise;
import org.nicocube.airain.domain.client.character.GameCharacter;
import org.nicocube.airain.domain.client.character.Possession;
import org.nicocube.airain.domain.client.character.SkillIdentifier;
import org.nicocube.airain.domain.client.data.StorageException;
import org.nicocube.airain.domain.client.gamedate.BirthDate;
import org.nicocube.airain.domain.client.gamedate.GameDate;
import org.nicocube.airain.domain.server.data.ContainerProviderImpl;
import org.nicocube.airain.domain.server.data.OrderedStorage;
import org.nicocube.airain.domain.server.data.StorageProvider;
import org.nicocube.airain.ds.CommonDSTest;
import org.nicocube.airain.ds.DSClient;
import org.nicocube.airain.utils.config.Config;
import org.nicocube.airain.utils.config.ConfigProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.db4o.ObjectContainer;

public class DataStorageBuilder extends CommonDSTest {
	
	private static final Logger log = LoggerFactory.getLogger(DataStorageBuilder.class);
	
	private static final int NB_INSERT = 10;

	private static StorageProvider storageProvider;
	
	@BeforeClass
	public static void prepare() throws Exception {
		Config c = ConfigProvider.inst().provide("storage.properties");

		ObjectContainer tmpdb;

		log.info("conf Action");
		tmpdb = DSClient.open(c);
		tmpdb.close();

		log.info("conf GameCharacter");
		tmpdb = DSClient.open(c);
		tmpdb.close();
		
		ContainerProviderImpl cp = new ContainerProviderImpl(c);
		
		storageProvider = new StorageProvider(cp);
	}

	@Test
	public void buildGameCharacterForTest() throws StorageException {
		log.info("buildGameCharacterForTest");

		OrderedStorage<GameCharacter> db = storageProvider.getStorage(GameCharacter.class);
		GameCharacter gc = new GameCharacter("Bandor",new BirthDate(1,1,1,1));

		gc.setSkillExpertise(SkillIdentifier.Hunting,Expertise.Master);
		
		gc.addPossession(
				new Possession(Possession.Type.javelin,new BirthDate(1,1,1,1))
		);
		gc.addPossession(
				new Possession(Possession.Type.food,new BirthDate(1,1,1,1))
		);
		
		gc.setLivedate(new GameDate(12,5,7,20));
		
		db.save(gc);
		db.commit();
	}

	@Test
	public void buildActionForTest() throws StorageException {
		log.info("buildActionForTest");
		OrderedStorage<Action> db = storageProvider.getStorage(Action.class);
		ActionType[] type = ActionType.values();

		Set<String> s = new HashSet<String>();

		for (int i = 0; i<NB_INSERT;i++) {
			//if (i%(NB_INSERT/10)==0) Log.debug(i/(NB_INSERT/10)+"0%");
			Random r = new Random();
			long gcO = 1;
			int m = 1;
			int y = 1; //r.nextInt(10000)+1;
			GameDate g;
			do {
				int h = r.nextInt(24)+1;
				int d = r.nextInt(3)+1;

				g = new GameDate(h, d, m, y);

			} while (s.contains(g.toString()));
			log.info("adding g="+g);
			s.add(g.toString());
						
			db.save(Action.build(type[r.nextInt(type.length)], gcO, g));
		}
		db.commit();
	}
}
